<?php

namespace plugin\muadmin\app\admin\controller;

use plugin\muadmin\app\common\base\BaseAdminController;
use plugin\muadmin\app\common\model\Attachment;
use Webman\Event\Event;

class Ajax extends BaseAdminController
{
//    protected $noNeedLogin = ['*'];
//    protected $noNeedRight = ['*'];
    public function upload()
    {
        $file = $this->request->file('file');
        $config = config('plugin.muadmin.upload');
        $event_config = Event::emit('get_upload_config',[]);
        foreach ($event_config as $item){
            if(!empty($item['upload'])&&is_array($item['upload'])){
                $config = array_merge($config,$item['upload']);
            }
        }
        if(!$file->isValid()){
            return $this->error("上传文件无效");
        }
//        print_r($this->request->header());
//        $file->move()
        try {
            $res = upload_file($config,$file);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
        $res['uploadtime'] = time();
        $res['admin_id'] = $this->auth->id;
        Attachment::create($res);
        return $this->success("上传成功",[
            'url' => $res['url'],
            'fullurl' => $res['fullurl'],
        ]);
    }
}